<template>
  <div class="textbox">
    <el-dropdown @command="handleCommand">
      <!-- <vab-icon icon="translate" /> -->
      <div class="text">{{ languagestr == 'zh' ? '中文' : 'English' }}</div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="zh">中文简体</el-dropdown-item>
          <el-dropdown-item command="en">English</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
  import { defineComponent, computed } from 'vue'
  import { useStore } from 'vuex'
  import { useRoute } from 'vue-router'
  import getPageTitle from '@/utils/pageTitle'
  import i18n from '@/i18n'

  export default defineComponent({
    name: 'VabLanguage',
    setup() {
      const store = useStore()
      const route = useRoute()

      const changeLanguage = (language) =>
        store.dispatch('settings/changeLanguage', language)
      const handleCommand = (language) => {
        console.log(language)
        changeLanguage(language)
        i18n.global.locale = language
        document.title = getPageTitle(route.meta.title)
      }

      return {
        handleCommand,
        languagestr: computed(() => store.getters['settings/language']),
      }
    },
  })
</script>
<style scoped>
  .text {
    color: #2c57f1;
    font-size: 16px;
    cursor: pointer;
  }
  .textbox {
    position: fixed;
    right: 40px;
    top: 20px;
    z-index: 999;
  }
</style>
